package com.suixun.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.dao.CustomReportMapper;
import com.suixun.entity.CustomReport;
import com.suixun.entity.form.OnlinepayForm;
import com.suixun.entity.form.ParamForm;
import com.suixun.entity.form.ReportActiveForm;
import com.suixun.service.DeviceService;
import com.suixun.service.OnlinepayService;
import com.suixun.service.ReportService;
import com.suixun.util.Constant;
import com.suixun.util.DateUtil;
import com.suixun.util.StringUtil;
import com.suixun.util.enums.Payment;
import com.suixun.vo.DiscountOrderVO;
import com.suixun.vo.ReportActiveVO;

@Controller
@RequestMapping(value = "/reportController")
public class ReportController extends BaseManager {
    @Resource
    private ReportService reportService;
    @Resource
    private DeviceService deviceService;
    @Resource
    private OnlinepayService onlinepayService;
    @Resource
    private CustomReportMapper customReportMapper;
    
    /**
     * 
     * <p>Title: addReport</p>
     * <p> 功能描述:新增自定义报表 </p>
     * @param param
     * @return
     */
    @RequestMapping(value = "addReport")
    @ResponseBody
    public  Object addReport(String param) {
    	boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null){
			return fail(Constant.EMPTY);
		}
		try {
			ReportActiveForm reportActiveForm = new Gson().fromJson(param, ReportActiveForm.class);
			if(!StringUtil.isNotEmpty(reportActiveForm.getName())){
				return fail(Constant.EMPTY);
			}
			reportActiveForm.setUsername(getUsername());
			reportActiveForm.setUserid(Integer.parseInt(getUserId().toString()));
			int isAdd=reportService.addReport(reportActiveForm);
			if(isAdd==1){
				Map<String,Object> result=new HashMap<>();
				result.put("result",Constant.OPERATE_SUCCESS);
				return success(result);
			}else{
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("ReportController addReport:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
    }
    
    /**
     * 
     * <p>Title: deleteReport</p>
     * <p> 功能描述:删除自定义报表参数列表 </p>
     * @param param
     * @return
     */
    @RequestMapping(value = "deleteReport.json")
    @ResponseBody
    public  Object deleteReport(String param) {
    	boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null){
			return fail(Constant.EMPTY);
		}
		try {
			ReportActiveForm reportActiveForm = new Gson().fromJson(param, ReportActiveForm.class);
			if(!StringUtil.isNotEmpty(reportActiveForm.getReportid())){
				return fail(Constant.EMPTY);
			}
			reportActiveForm.setUserid(Integer.parseInt(getUserId().toString()));
			int isDelete=reportService.deleteReport(reportActiveForm);
			if(isDelete==1){
				Map<String,Object> result=new HashMap<>();
				result.put("result",Constant.OPERATE_SUCCESS);
				return success(result);
			}else{
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("ReportController deleteReport:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
    }
    /**
     * 
     * <p>Title: queryCustomReportList</p>
     * <p> 功能描述:查询自定义报表参数列表 </p>
     * @param param
     * @return
     */
    @RequestMapping(value = "queryCustomReportList.json")
    @ResponseBody
    public  Object queryCustomReportList(String param) {
    	try {
    		ReportActiveForm reportActiveForm = new Gson().fromJson(param, ReportActiveForm.class);
			reportActiveForm.setUserid(Integer.parseInt(getUserId().toString()));
			return success(reportService.queryCustomReportByPaging(reportActiveForm));
		} catch (Exception e) {
			logger("ReportController queryCustomReportList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
    }
    /**
     * 
     * <p>Title: exportReportList</p>
     * <p> 功能描述:导出excel </p>
     * @param param
     * @return
     */
    @RequestMapping(value = "export_reportList.json")
    @ResponseBody
    public  Object exportReportList(String param) {
    	boolean bln = checkRight();
    	if (!bln) {
    		toRedirect(404);
    	}
    	if(param==null){
			return fail(Constant.EMPTY);
		}
    	try {
    		ReportActiveVO reportActiveForm = new Gson().fromJson(param, ReportActiveVO.class);
        	boolean check = true;
        	if (reportActiveForm != null) {
        		check = reportActiveForm.getReporttype() == null && reportActiveForm.getReportid() == null;
        	}
        	HSSFWorkbook workbook = null;
        	HttpServletResponse response = this.getResponse();
        	if (reportActiveForm == null || check) {
        		workbook = deviceService.exportDeviceList(new ParamForm());
        		response.setHeader("Content-disposition", "attachment;filename=deviceReport.xls");
        	}else if (reportActiveForm.getReporttype() != null) {
        		workbook = onlinepayService.exportOnlinepayList(new OnlinepayForm());
        		response.setHeader("Content-disposition", "attachment;filename=onlinepayReport.xls");
        	}else if (reportActiveForm.getReportid() != null) {
        		CustomReport customReport = customReportMapper.selectByPrimaryKey(reportActiveForm.getReportid());
        		//缴费明细表
        		if (customReport.getReporttype() == 1) {
        			ParamForm deviceVo = new ParamForm();
        			if (customReport.getDevicetype() != null) {
        				if (customReport.getDevicetype() == 1) {
        					deviceVo.setDevicetype("9");
        				}else if (customReport.getDevicetype() == 2) {
        					deviceVo.setDevicetype("2");
        				}else if (customReport.getDevicetype() == 3) {
        					deviceVo.setDevicetype("8");
        				}
        			}
        			deviceVo.setSchoolname(customReport.getSchoolname());
        			deviceVo.setStarttime(DateUtil.formatDateToString(customReport.getStarttime(), null));
        			deviceVo.setEndtime(DateUtil.formatDateToString(customReport.getEndtime(), null));
        			deviceVo.setTeachertel(customReport.getTeachertel());
        			if (customReport.getDevicestatus() != null && customReport.getDevicestatus() != 0) {
        				if (customReport.getDevicestatus() == 5) {
        					deviceVo.setStatus("6");
        				}else {
        					deviceVo.setStatus(customReport.getDevicestatus() == null ? null : customReport.getDevicestatus().toString());
        				}
        			}
        			deviceVo.setProvince(customReport.getProvince());
        			deviceVo.setCity(customReport.getCity());
        			deviceVo.setTown(customReport.getTown());
        			workbook = deviceService.exportDeviceList(deviceVo);
        			response.setHeader("Content-disposition", "attachment;filename=deviceReport.xls");
        		//app购买订单
        		}else if (customReport.getReporttype() == 2) {
        			OnlinepayForm onlinepay = new OnlinepayForm();
        			if (customReport.getOrderstatus() != null && customReport.getOrderstatus() != 0) {
        				onlinepay.setStatus(customReport.getOrderstatus() == null ? null : customReport.getOrderstatus().toString());
        			}
        			onlinepay.setStarttime(DateUtil.formatDateToString(customReport.getStarttime(), null));
        			onlinepay.setEndtime(DateUtil.formatDateToString(customReport.getEndtime(), null));
        			if (customReport.getDevicetype() != null) {
        				if (customReport.getDevicetype() == 1) {
        					onlinepay.setDevicetype(9);
        				}else if (customReport.getDevicetype() == 2) {
        					onlinepay.setDevicetype(2);
        				}else if (customReport.getDevicetype() == 3) {
        					onlinepay.setDevicetype(8);
        				}
        			}
        			workbook = onlinepayService.exportOnlinepayList(onlinepay);
        			response.setHeader("Content-disposition", "attachment;filename=onlinepayReport.xls");
        		//优惠促销
        		}else {
        			DiscountOrderVO discountOrder = new DiscountOrderVO();
        			if (customReport.getOrderstatus() != null && customReport.getOrderstatus() != 0) {
        				discountOrder.setSendstatus(customReport.getOrderstatus());
        			}
        			discountOrder.setStarttime(DateUtil.formatDateToString(customReport.getStarttime(), null));
        			discountOrder.setEndtime(DateUtil.formatDateToString(customReport.getEndtime(), null));
        			discountOrder.setSchoolname(customReport.getSchoolname());
        			discountOrder.setProvince(customReport.getProvince());
        			discountOrder.setCity(customReport.getCity());
        			discountOrder.setTown(customReport.getTown());
        			workbook = onlinepayService.exportDiscountList(discountOrder);
        			response.setHeader("Content-disposition", "attachment;filename=discountReport.xls");
        		}
        	}
    		workbook.write(this.getResponse().getOutputStream());
    		workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger("ReportController exportReportList:"+e.getMessage(),e);
			toRedirect(500);
		}
    	return success();
    }
    
    /**
     * 
     * <p>Title: exportFinanceReport</p>
     * <p> 功能描述:导出财务报表 </p>
     * @param param
     * @return
     */
    @RequestMapping(value = "exportFinanceReport.json")
    @ResponseBody
    public  Object exportFinanceReport(String param) {
    	try {
    		ReportActiveForm reportActiveForm = checkParam(param, ReportActiveForm.class);
        	HttpServletResponse response = this.getResponse();
        	if (StringUtil.isEmpty(reportActiveForm.getReporttype())) {
        		return fail("报表类型不能为空");
        	}
        	HSSFWorkbook workbook = null;
        	if ("yiji".equals(reportActiveForm.getReporttype())) {
        		reportActiveForm.setPayment(Payment.YIJI);
        		workbook = reportService.exportYijiReport(reportActiveForm);
        		response.setHeader("Content-disposition", "attachment;filename=yijifuReport.xls");
        	}else if ("wechat".equals(reportActiveForm.getReporttype())) {
        		reportActiveForm.setPayment(Payment.WECHAT);
        		workbook = reportService.exportWeichatReport(reportActiveForm);
        		response.setHeader("Content-disposition", "attachment;filename=weichatReport.xls");
        	}else {
        		return fail("报表类型不符合规范");
        	}
    		workbook.write(this.getResponse().getOutputStream());
    		workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger("ReportController exportFinanceReport:"+e.getMessage(),e);
			toRedirect(500);
		}
    	return success();
    }
}
